﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SilverlightDemo.Web.Models
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MonHocService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select MonHocService.svc or MonHocService.svc.cs at the Solution Explorer and start debugging.
    public class MonHocService : IMonHocService
    {
        public List<MonHoc> GetAllMonHoc()
        {
            var data = new DataClasses1DataContext();
            return data.MonHocs.ToList();
        }


        public Boolean InsertMonHoc(ref MonHoc  info)
        {            
            var data = new DataClasses1DataContext();
            MonHoc newinfo = new MonHoc();
            try
            {
               
                newinfo.Tenmh = info.Tenmh;
                newinfo.Sotc = info.Sotc;
                newinfo.Hocphi = info.Hocphi;
                data.MonHocs.InsertOnSubmit(newinfo);
                data.SubmitChanges();
                info.Mamh = newinfo.Mamh;
                return true;
            }
            catch (Exception ex)
            {
                info.Mamh = newinfo.Mamh;
                return false;
            }
        }
        public Boolean DeleteMonHoc(String id)
        {
            var data = new DataClasses1DataContext();
            try
            {
                var re = (from mh in data.MonHocs where mh.Mamh == int.Parse(id) select mh).Single();
                if (re == null) return false;
                data.MonHocs.DeleteOnSubmit(re);
                data.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public Boolean UpdateMonHoc(MonHoc info)
        {
            var data = new DataClasses1DataContext();
            try
            {
                var re = (from mh in data.MonHocs where mh.Mamh == info.Mamh select mh).Single();
                if (re == null) return false;
                re.Tenmh = info.Tenmh;
                re.Sotc = info.Sotc;
                re.Hocphi = info.Hocphi;
                data.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}
